package com.eshop.inventory.request;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: Xukai
 * @description: 请求内存队列
 * @createDate: 2018/7/23 11:49
 * @modified By:
 */
public class RequestQueue {

    private List<ArrayBlockingQueue<Request>> queues = new ArrayList<>();

    /**
     * 标识位
     */
    private ConcurrentHashMap<Integer, Boolean> flagMap = new ConcurrentHashMap<>();

    private static class Singleton {

        private static RequestQueue instance;

        static {
            instance = new RequestQueue();
        }

        public static RequestQueue getInstance() {
            return instance;
        }
    }

    public static RequestQueue getInstance() {
        return Singleton.getInstance();
    }

    /**
     * 添加一个内存队列
     *
     * @param queue
     */
    public void addQueue(ArrayBlockingQueue<Request> queue) {
        queues.add(queue);
    }

    /**
     * 获得内存队列的数量
     *
     * @return
     */
    public int getQueueSize() {
        return queues.size();
    }

    /**
     * 获取内存队列
     *
     * @param index
     * @return
     */
    public ArrayBlockingQueue<Request> getQueue(int index) {
        return queues.get(index);
    }

    /**
     * 获取标识位map
     * 如果标识不为空，而且是true，说明之前有一个这个商品的数据库更新请求刚被执行完
     * 此时缓存是空的，需要进行缓存更新
     * @return
     */
    public ConcurrentHashMap<Integer, Boolean> getFlagMap(){
        return flagMap;
    }
}
